import base from "@/config/baseUrl";

class Commond {
  constructor() {
    this.servDomain = "https://app.diaochan88.com/";
  }
  tips(title) {
    uni.showToast({
      title,
      icon: "none",
    });
  }
  post(path, param = null, method = "POST", header = {}) {
    const that = this;
    const userInfo = uni.getStorageSync("userInfo");
    const location = uni.getStorageSync("location");
    return new Promise((resolve, reject) => {
      uni.showLoading({
        title: "loading...",
        mask: true,
      });
      uni.request({
        url: `${that.servDomain}${path}`,
        method: method,
        header: {
			"content-type": "application/json",
			token: userInfo?userInfo.token:undefined
		},
        data: param,
        success: (res) => {
          // console.log(path, res);
		  
		  if(res.statusCode && res.statusCode == 500){
			  // 登录过期
			 uni.navigateTo({
			   url: "/pages/user/login22",
			 });
			 return;
		  }

          if (res.statusCode && res.statusCode == 200) {
            let httpData = res.data;
            if (typeof httpData == "string") {
              httpData = JSON.parse(httpData);
            }
            if (
              httpData.msg === "请先登录" ||
              httpData.msg === "登录已过期，请重新登录"
            ) {
              setTimeout(() => {
                uni.navigateTo({
                  url: "/pages/user/login22",
                });
              }, 100);
              return;
            }
            //判断数据是否请求成功
            if (
              httpData.success ||
              httpData.code == 200 ||
              httpData.code == 1 ||
              httpData.code == 2 ||
              httpData.code == 0
            ) {
              // 返回正确的结果(then接受数据)
			  // console.log("httpData.data", httpData.data);
              return resolve(httpData.data);
            } else if (
              httpData.code == "1000" ||
              httpData.code == "1001" ||
              httpData.code == 1100 ||
              httpData.code == 402
            ) {
              // store.commit("emptyUserInfo");
              // #ifdef APP-PLUS
              var content = "此时此刻需要您登录喔~";
              if (httpData.code == "1000") {
                content = "此时此刻需要您登录喔";
              }
              if (loginPopupNum <= 0) {
                loginPopupNum++;
                uni.showModal({
                  title: "温馨提示",
                  content: content,
                  confirmText: "去登录",
                  cancelText: "再逛会",
                  success: function (res) {
                    loginPopupNum--;
                    if (res.confirm) {
                      uni.navigateTo({
                        url: "/pages/user/login22",
                      });
                    }
                  },
                });
              }
              // #endif
              // 返回错误的结果(catch接受数据)
              return reject({
                statusCode: 0,
                errMsg: httpData.info || httpData.msg,
                data: res.data,
              });
            } else if (httpData.code == "1004") {
              if (loginPopupNum <= 0) {
                loginPopupNum++;
                uni.showModal({
                  title: "提示",
                  content: "您还未绑定手机号，请先绑定~",
                  confirmText: "去绑定",
                  cancelText: "再逛会",
                  success: (res) => {
                    loginPopupNum--;
                    if (res.confirm) {
                      uni.navigateTo({
                        url: "/pages/user/bindPhone",
                      });
                    }
                  },
                });
              }
              // 返回错误的结果(catch接受数据)
              return reject({
                statusCode: 0,
                errMsg: httpData.info || httpData.msg,
                data: res.data,
              });
            } else {
              //其他错误提示
              if (res.isPrompt) {
                uni.showToast({
                  title: httpData.info || httpData.msg,
                  // title: '在这里',
                  icon: "none",
                  duration: 3000,
                });
              }
              // 返回错误的结果(catch接受数据)
              return reject({
                statusCode: 0,
                errMsg: httpData.info || httpData.msg,
                data: res.data,
              });
            }
          } else {
            // 返回错误的结果(catch接受数据)
            return reject({
              statusCode: res.statusCode,
              errMsg: "【request】数据工厂验证不通过",
              data: res.data,
            });
          }
        },
        fail: (err) => {
          console.log(err);
          uni.showToast({
            icon: "error",
            title: "网络连接失败",
            mask: true,
            duration: 1500,
          });
          reject(err);
        },
        complete: () => {
          uni.hideLoading();
        },
      });
    });
  }
  formatTime(time, fmtStr = 0) {
    var date = new Date(time * 1000),
      year = date.getFullYear(),
      month = date.getMonth() + 1,
      day = date.getDate(),
      hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
      minute =
        date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
      second =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    month >= 1 && month <= 9 ? (month = "0" + month) : "";
    day >= 0 && day <= 9 ? (day = "0" + day) : "";

    return { year, month, day, hour, minute, second };
  }
  getLocalPos() {
    return new Promise((resolve, reject) => {
      uni.getLocation({
        type: "wgs84",
        success: (res) => {
          const location = { lat: res.latitude, lng: res.longitude };
          resolve(location);
        },
        fail(e) {
          console.log(e);
          reject(e);
        },
      });
    });
  }
}

export default new Commond();
